package com.example.usermanagement.liuka.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.usermanagement.exception.ParamErrorException;
import com.example.usermanagement.liuka.model.SysUser;
import com.example.usermanagement.liuka.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;


/**
 * 系统管理
 */
@AllArgsConstructor
@RestController
@RequestMapping("/sys-user")
@Validated
public class SysUserController {

    private final SysUserService userService;


    /**
     * 创建系统管理用户
     * @param username 账户
     * @param password 密码
     * @param name  姓名
    **/
    @PostMapping()
    public void addSysUser(@NotBlank(message = "username不能为空")String username,
                           @NotBlank(message = "密码不能为空")String password,
                           @NotBlank(message = "确认密码不能为空") String commitPassword,
                           String name) throws ParamErrorException {

        if(!password.trim().equals(commitPassword.trim())){
            throw new ParamErrorException("两次输入密码不一致");
        }

        SysUser sysUser = SysUser.builder().username(username)
                .password(password)
                .name(name).build();
        userService.save(sysUser);

    }


    /**
     * 系统管理员 分页列表
     * @param page  第几页 默认1
     * @param pageSize 每页条数 默认20
     * @return
    **/
    @GetMapping("/page")
    public IPage<SysUser> getPage(@RequestParam(defaultValue = "1")Long page,
                                  @RequestParam(defaultValue = "20")Long pageSize){
        return userService.page(new Page<>(page,pageSize));
    }


}
